<!DOCTYPE HTML>

<html>
    <head>
        <script type="application/ld+json">
    {
        "@context" : "http://schema.org",
        "@type" : "BlogPosting",
        "mainEntityOfPage": {
             "@type": "WebPage",
             "@id": "\/blog\/"
        },
        "articleSection" : "blog",
        "name" : "Django模型及迁移文件",
        "headline" : "Django模型及迁移文件",
        "description" : "mff的个人博客",
        "inLanguage" : "en",
        "author" : "",
        "creator" : "",
        "publisher": "",
        "accountablePerson" : "",
        "copyrightHolder" : "",
        "copyrightYear" : "2020",
        "datePublished": "2020-04-28 00:00:00 \x2b0000 UTC",
        "dateModified" : "2020-04-28 00:00:00 \x2b0000 UTC",
        "url" : "\/blog\/blog\/djangomodels%E6%A8%A1%E5%9E%8B%E5%8F%8A%E8%BF%81%E7%A7%BB\/",
        "wordCount" : "222",
        "keywords" : [ "tutorial","Blog" ]
    }
    </script>
        
            
                <title>Django模型及迁移文件</title>
            
        

        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <meta name="generator" content="Hugo 0.69.0" />
        
  
    
  

  

  <link rel="apple-touch-icon-precomposed" href='/blog/favicon/apple-touch-icon-precomposed.png'>
  <link rel="icon" href='/blog/favicon/favicon.png'>
  
  <meta name="msapplication-TileColor" content="#da532c">
  <meta name="msapplication-TileImage" content='/favicon/mstile.png'>
  <meta name="application-name" content="mff blog">
  <meta name="msapplication-tooltip" content="">
  <meta name="msapplication-config" content='/favicon/ieconfig.xml'>



        
            <meta name="author" content="mff">
        
        
            <meta name="description" content="mff的个人博客">
        

        <meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="Django模型及迁移文件"/>
<meta name="twitter:description" content="mff的个人博客"/>

        <meta property="og:title" content="Django模型及迁移文件" />
<meta property="og:description" content="mff的个人博客" />
<meta property="og:type" content="article" />
<meta property="og:url" content="/blog/blog/djangomodels%E6%A8%A1%E5%9E%8B%E5%8F%8A%E8%BF%81%E7%A7%BB/" />
<meta property="article:published_time" content="2020-04-28T00:00:00+00:00" />
<meta property="article:modified_time" content="2020-04-28T00:00:00+00:00" />

        <meta property="og:image" content="/blog//images/logo.png">
        <meta property="og:image:type" content="image/png">
        <meta property="og:image:width" content="512">
        <meta property="og:image:height" content="512">
        <meta itemprop="name" content="Django模型及迁移文件">
<meta itemprop="description" content="mff的个人博客">
<meta itemprop="datePublished" content="2020-04-28T00:00:00&#43;00:00" />
<meta itemprop="dateModified" content="2020-04-28T00:00:00&#43;00:00" />
<meta itemprop="wordCount" content="222">



<meta itemprop="keywords" content="tutorial," />
        

        
            
        

        
        
            <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/atom-one-light.min.css">
            <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway:400,800,900|Source+Sans+Pro:400,700">
            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
            <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.1.25/jquery.fancybox.min.css">
            <link rel="stylesheet" href="/blog/css/main.css">
            <link rel="stylesheet" href="/blog/css/add-on.css">
            <link rel="stylesheet" href="/blog/css/academicons.min.css">
        

        
            
                
            
        


  
    
    <link href='//cdn.bootcss.com/highlight.js/9.11.0/styles/github.min.css' rel='stylesheet' type='text/css' />
  


      
    </head>
    <body>

      
      <div id="wrapper">

    
    
<header id="header">
    
      <h1><a href="/blog/">blog</a></h1>
    

    <nav class="links">
        <ul>
            
                <li>
                    <a href="/blog/">
                            <i class="fa fa-home">&nbsp;</i>主页
                    </a>
                </li>
            
                <li>
                    <a href="/blog/blog/">
                            <i class="fa fa-newspaper-o">&nbsp;</i>Blog笔记
                    </a>
                </li>
            
                <li>
                    <a href="https://mff-project.gitee.io/hugoblog/posts/%E4%B8%AA%E4%BA%BA%E7%AE%80%E5%8E%86/">
                            <i class="fa fa-id-card-o">&nbsp;</i>个人简历
                    </a>
                </li>
            
                <li>
                    <a href="/blog/itemized/">
                            <i class="fa fa-list">&nbsp;</i>最近项目
                    </a>
                </li>
            
                <li>
                    <a href="/blog/contact/">
                            <i class="fa fa-envelope-o">&nbsp;</i>联系我
                    </a>
                </li>
            
        </ul>
    </nav>
    <nav class="main">
        <ul>
            
            <li id="share-nav" class="share-menu" style="display:none;">
                <a class="fa-share-alt" href="#share-menu">Share</a>
            </li>
            
            <li class="search">
                <a class="fa-search" href="#search">Search</a>
                <form id="search" method="get" action="//google.com/search">
                    <input type="text" name="q" placeholder="Search" />
                    <input type="hidden" name="as_sitesearch" value="/blog/">
                </form>
            </li>
            <li class="menu">
                <a class="fa-bars" href="#menu">Menu</a>
            </li>
        </ul>
    </nav>
</header>


<section id="menu">

    
        <section>
            <form class="search" method="get" action="//google.com/search">
                <input type="text" name="q" placeholder="Search" />
                <input type="hidden" name="as_sitesearch" value="/blog/">
            </form>
        </section>

    
        <section>
            <ul class="links">
                
                    <li>
                        <a href="/blog/">
                            <h3>
                                <i class="fa fa-home">&nbsp;</i>主页
                            </h3>
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/blog/">
                            <h3>
                                <i class="fa fa-newspaper-o">&nbsp;</i>Blog笔记
                            </h3>
                        </a>
                    </li>
                
                    <li>
                        <a href="https://mff-project.gitee.io/hugoblog/posts/%E4%B8%AA%E4%BA%BA%E7%AE%80%E5%8E%86/">
                            <h3>
                                <i class="fa fa-id-card-o">&nbsp;</i>个人简历
                            </h3>
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/itemized/">
                            <h3>
                                <i class="fa fa-list">&nbsp;</i>最近项目
                            </h3>
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/contact/">
                            <h3>
                                <i class="fa fa-envelope-o">&nbsp;</i>联系我
                            </h3>
                        </a>
                    </li>
                
            </ul>
        </section>

    
        <section class="recent-posts">
            <div class="mini-posts">
                <header>
                    <h3>Recent Posts</h3>
                </header>
                

                
                    
                

                
                        <article class="mini-post">
                            <header>
                                <h3><a href="/blog/blog/fastdfs%E9%85%8D%E7%BD%AE%E5%8F%8A%E4%BB%8B%E7%BB%8D/">FastDFS配置及介绍</a></h3>
                                
                                <time class="published" datetime=
                                    '2020-08-27'>
                                    August 27, 2020</time>
                            </header>
                            

                        </article>
                
                        <article class="mini-post">
                            <header>
                                <h3><a href="/blog/blog/orm%E5%B8%B8%E8%A7%84%E6%93%8D%E4%BD%9C%E9%87%8D%E7%82%B9/">orm常规操作</a></h3>
                                
                                <time class="published" datetime=
                                    '2020-07-28'>
                                    July 28, 2020</time>
                            </header>
                            

                        </article>
                
                        <article class="mini-post">
                            <header>
                                <h3><a href="/blog/blog/%E6%94%AF%E4%BB%98%E5%AE%9D%E6%B2%99%E7%AE%B1%E6%94%AF%E4%BB%98%E6%93%8D%E4%BD%9C/">支付宝沙箱支付操作</a></h3>
                                
                                <time class="published" datetime=
                                    '2020-07-28'>
                                    July 28, 2020</time>
                            </header>
                            

                        </article>
                
                        <article class="mini-post">
                            <header>
                                <h3><a href="/blog/blog/%E8%85%BE%E8%AE%AF%E7%9F%AD%E4%BF%A1%E5%8F%91%E9%80%81%E9%AA%8C%E8%AF%81%E7%A0%81/">腾讯短信发送验证码</a></h3>
                                
                                <time class="published" datetime=
                                    '2020-07-28'>
                                    July 28, 2020</time>
                            </header>
                            

                        </article>
                
                        <article class="mini-post">
                            <header>
                                <h3><a href="/blog/blog/flask&#43;vue%E5%AE%9E%E7%8E%B0%E5%89%8D%E5%90%8E%E7%AB%AF%E5%88%86%E7%A6%BB/">flask&#43;vue实现前后端分离</a></h3>
                                
                                <time class="published" datetime=
                                    '2020-07-26'>
                                    July 26, 2020</time>
                            </header>
                            

                        </article>
                

                
                    <a href=
                        
                            /blog/
                        
                        class="button">View more posts</a>
                
            </div>
        </section>

    
        
</section>

    <section id="share-menu">
    <section id="social-share-nav">
        <ul class="links">
            <header>
                <h3>Share this post <i class="fa fa-smile-o"></i></h3>
            </header>
            



<li>
  <a href="//twitter.com/share?url=%2fblog%2fblog%2fdjangomodels%25E6%25A8%25A1%25E5%259E%258B%25E5%258F%258A%25E8%25BF%2581%25E7%25A7%25BB%2f&amp;text=Django%e6%a8%a1%e5%9e%8b%e5%8f%8a%e8%bf%81%e7%a7%bb%e6%96%87%e4%bb%b6&amp;via=" target="_blank" class="share-btn twitter">
    <i class="fa fa-twitter"></i>
    <p>Twitter</p>
    </a>
</li>




<li>
  <a href="//plus.google.com/share?url=%2fblog%2fblog%2fdjangomodels%25E6%25A8%25A1%25E5%259E%258B%25E5%258F%258A%25E8%25BF%2581%25E7%25A7%25BB%2f" target="_blank" class="share-btn google-plus">
    <i class="fa fa-google-plus"></i>
    <p>Google+</p>
  </a>
</li>





<li>
  <a href="//www.facebook.com/sharer/sharer.php?u=%2fblog%2fblog%2fdjangomodels%25E6%25A8%25A1%25E5%259E%258B%25E5%258F%258A%25E8%25BF%2581%25E7%25A7%25BB%2f" target="_blank" class="share-btn facebook">
    <i class="fa fa-facebook"></i>
    <p>Facebook</p>
    </a>
</li>




<li>
  <a href="//reddit.com/submit?url=%2fblog%2fblog%2fdjangomodels%25E6%25A8%25A1%25E5%259E%258B%25E5%258F%258A%25E8%25BF%2581%25E7%25A7%25BB%2f&amp;title=Django%e6%a8%a1%e5%9e%8b%e5%8f%8a%e8%bf%81%e7%a7%bb%e6%96%87%e4%bb%b6" target="_blank" class="share-btn reddit">
    <i class="fa fa-reddit-alien"></i>
    <p>Reddit</p>
  </a>
</li>




<li>
  <a href="//www.linkedin.com/shareArticle?url=%2fblog%2fblog%2fdjangomodels%25E6%25A8%25A1%25E5%259E%258B%25E5%258F%258A%25E8%25BF%2581%25E7%25A7%25BB%2f&amp;title=Django%e6%a8%a1%e5%9e%8b%e5%8f%8a%e8%bf%81%e7%a7%bb%e6%96%87%e4%bb%b6" target="_blank" class="share-btn linkedin">
      <i class="fa fa-linkedin"></i>
      <p>LinkedIn</p>
    </a>
</li>




<li>
  <a href="//www.stumbleupon.com/submit?url=%2fblog%2fblog%2fdjangomodels%25E6%25A8%25A1%25E5%259E%258B%25E5%258F%258A%25E8%25BF%2581%25E7%25A7%25BB%2f&amp;title=Django%e6%a8%a1%e5%9e%8b%e5%8f%8a%e8%bf%81%e7%a7%bb%e6%96%87%e4%bb%b6" target="_blank" class="share-btn stumbleupon">
    <i class="fa fa-stumbleupon"></i>
    <p>StumbleUpon</p>
  </a>
</li>




<li>
  <a href="//www.pinterest.com/pin/create/button/?url=%2fblog%2fblog%2fdjangomodels%25E6%25A8%25A1%25E5%259E%258B%25E5%258F%258A%25E8%25BF%2581%25E7%25A7%25BB%2f&amp;description=Django%e6%a8%a1%e5%9e%8b%e5%8f%8a%e8%bf%81%e7%a7%bb%e6%96%87%e4%bb%b6" target="_blank" class="share-btn pinterest">
    <i class="fa fa-pinterest-p"></i>
    <p>Pinterest</p>
  </a>
</li>




<li>
  <a href="mailto:?subject=Check out this post by mff&amp;body=%2fblog%2fblog%2fdjangomodels%25E6%25A8%25A1%25E5%259E%258B%25E5%258F%258A%25E8%25BF%2581%25E7%25A7%25BB%2f" target="_blank" class="share-btn email">
    <i class="fa fa-envelope"></i>
    <p>Email</p>
  </a>
</li>


        </ul>
    </section>
</section>

    
    <div id="main">
        
        
        <article class="post">
  <header>
    <div class="title">
        
            <h1><a href="/blog/blog/djangomodels%E6%A8%A1%E5%9E%8B%E5%8F%8A%E8%BF%81%E7%A7%BB/">Django模型及迁移文件</a></h1>
            
        
        
            <p>mff的个人博客</p>
        
    </div>
    <div class="meta">
        

        <time class="published"
            datetime='2020-04-28'>
            April 28, 2020</time>
        <span class="author">mff</span>
        
            <p>2 minute read</p>
        
        
    </div>
</header>


  
    <section id="social-share">
      <ul class="icons">
        



<li>
  <a href="//twitter.com/share?url=%2fblog%2fblog%2fdjangomodels%25E6%25A8%25A1%25E5%259E%258B%25E5%258F%258A%25E8%25BF%2581%25E7%25A7%25BB%2f&amp;text=Django%e6%a8%a1%e5%9e%8b%e5%8f%8a%e8%bf%81%e7%a7%bb%e6%96%87%e4%bb%b6&amp;via=" target="_blank" class="share-btn twitter">
    <i class="fa fa-twitter"></i>
    <p>Twitter</p>
    </a>
</li>




<li>
  <a href="//plus.google.com/share?url=%2fblog%2fblog%2fdjangomodels%25E6%25A8%25A1%25E5%259E%258B%25E5%258F%258A%25E8%25BF%2581%25E7%25A7%25BB%2f" target="_blank" class="share-btn google-plus">
    <i class="fa fa-google-plus"></i>
    <p>Google+</p>
  </a>
</li>





<li>
  <a href="//www.facebook.com/sharer/sharer.php?u=%2fblog%2fblog%2fdjangomodels%25E6%25A8%25A1%25E5%259E%258B%25E5%258F%258A%25E8%25BF%2581%25E7%25A7%25BB%2f" target="_blank" class="share-btn facebook">
    <i class="fa fa-facebook"></i>
    <p>Facebook</p>
    </a>
</li>




<li>
  <a href="//reddit.com/submit?url=%2fblog%2fblog%2fdjangomodels%25E6%25A8%25A1%25E5%259E%258B%25E5%258F%258A%25E8%25BF%2581%25E7%25A7%25BB%2f&amp;title=Django%e6%a8%a1%e5%9e%8b%e5%8f%8a%e8%bf%81%e7%a7%bb%e6%96%87%e4%bb%b6" target="_blank" class="share-btn reddit">
    <i class="fa fa-reddit-alien"></i>
    <p>Reddit</p>
  </a>
</li>




<li>
  <a href="//www.linkedin.com/shareArticle?url=%2fblog%2fblog%2fdjangomodels%25E6%25A8%25A1%25E5%259E%258B%25E5%258F%258A%25E8%25BF%2581%25E7%25A7%25BB%2f&amp;title=Django%e6%a8%a1%e5%9e%8b%e5%8f%8a%e8%bf%81%e7%a7%bb%e6%96%87%e4%bb%b6" target="_blank" class="share-btn linkedin">
      <i class="fa fa-linkedin"></i>
      <p>LinkedIn</p>
    </a>
</li>




<li>
  <a href="//www.stumbleupon.com/submit?url=%2fblog%2fblog%2fdjangomodels%25E6%25A8%25A1%25E5%259E%258B%25E5%258F%258A%25E8%25BF%2581%25E7%25A7%25BB%2f&amp;title=Django%e6%a8%a1%e5%9e%8b%e5%8f%8a%e8%bf%81%e7%a7%bb%e6%96%87%e4%bb%b6" target="_blank" class="share-btn stumbleupon">
    <i class="fa fa-stumbleupon"></i>
    <p>StumbleUpon</p>
  </a>
</li>




<li>
  <a href="//www.pinterest.com/pin/create/button/?url=%2fblog%2fblog%2fdjangomodels%25E6%25A8%25A1%25E5%259E%258B%25E5%258F%258A%25E8%25BF%2581%25E7%25A7%25BB%2f&amp;description=Django%e6%a8%a1%e5%9e%8b%e5%8f%8a%e8%bf%81%e7%a7%bb%e6%96%87%e4%bb%b6" target="_blank" class="share-btn pinterest">
    <i class="fa fa-pinterest-p"></i>
    <p>Pinterest</p>
  </a>
</li>




<li>
  <a href="mailto:?subject=Check out this post by mff&amp;body=%2fblog%2fblog%2fdjangomodels%25E6%25A8%25A1%25E5%259E%258B%25E5%258F%258A%25E8%25BF%2581%25E7%25A7%25BB%2f" target="_blank" class="share-btn email">
    <i class="fa fa-envelope"></i>
    <p>Email</p>
  </a>
</li>


      </ul>
    </section>
  

  

  <div id="content">
    <h2 id="django是什么">Django是什么</h2>
<ol>
<li>
<p><a href="https://www.leiue.com/tags/django">Django</a> 是一个开放源代码的 Web <a href="https://www.leiue.com/tags/%e5%ba%94%e7%94%a8%e6%a1%86%e6%9e%b6">应用框架</a>，由 <a href="https://www.leiue.com/tags/python">Python</a> 写成。采用了 MVT 的软件设计模式，即模型 Model，视图 View 和模板 Template。它最初是被开发来用于管理劳伦斯出版集团旗下的一些以新闻内容为主的网站的。并于 2005 年 7 月在 BSD 许可证下发布。</p>
</li>
<li>
<p>Django 的主要目标是使得开发复杂的、数据库驱动的网站变得简单。Django 注重组件的重用性和“可插拔性”，敏捷开发和 DRY 法则（Don’t Repeat Yourself）。在 Django 中 Python 被普遍使用，甚至包括配置文件和数据模型。</p>
</li>
<li>
<p>Django 框架的核心包括：一个 面向对象 的映射器，用作数据模型（以 Python 类的形式定义）和关系型数据库间的介质；一个基于正则表达式的 URL 分发器；一个视图系统，用于处理请求；以及一个模板系统。</p>
<p>核心框架中还包括：</p>
<ul>
<li>一个轻量级的、独立的 Web 服务器，用于开发和测试。</li>
<li>一个表单序列化及验证系统，用于 HTML 表单和适于数据库存储的数据之间的转换。</li>
<li>一个缓存框架，并有几种缓存方式可供选择。</li>
<li>中间件支持，允许对请求处理的各个阶段进行干涉。</li>
<li>内置的分发系统允许应用程序中的组件采用预定义的信号进行相互间的通信。</li>
<li>一个序列化系统，能够生成或读取采用 XML 或 JSON 表示的 Django 模型实例。</li>
<li>一个用于扩展模板引擎的能力的系统。</li>
</ul>
</li>
</ol>
<hr>
<h3 id="这里先做django中orm模型介绍">这里先做Django中ORM模型介绍</h3>
<ol>
<li>
<p><strong>对象关系映射（Object Relational Mapping，简称ORM）模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术。</strong></p>
<p>简单的说，ORM是通过使用描述对象和数据库之间映射的元数据，将程序中的对象自动持久化到关系数据库中。</p>
<p>ORM在业务<strong>逻辑层和数据库层</strong>之间充当了<strong>桥梁</strong>的作用。</p>
</li>
<li>
<p>models模型写法及应用</p>
<p>定义模型</p>
<p>定义属性:定义属性时，需要字段类型</p>
<ul>
<li>AutoField：一个根据实际ID自动增长的IntegerField，通常不指定
<ul>
<li>如果不指定，一个主键字段将自动添加到模型中</li>
</ul>
</li>
<li>BooleanField：true/false 字段，此字段的默认表单控制是CheckboxInput</li>
<li>NullBooleanField：支持null、true、false三种值</li>
<li>CharField(max_length=字符长度)：字符串，默认的表单样式是 TextInput</li>
<li>TextField：大文本字段，一般超过4000使用，默认的表单控件是Textarea</li>
<li>IntegerField：整数</li>
<li>DecimalField(max_digits=None, decimal_places=None)：使用python的Decimal实例表示的十进制浮点数
<ul>
<li>DecimalField.max_digits：位数总数</li>
<li>DecimalField.decimal_places：小数点后的数字位数</li>
</ul>
</li>
<li>FloatField：用Python的float实例来表示的浮点数</li>
<li>DateField[auto_now=False, auto_now_add=False])：使用Python的datetime.date实例表示的日期
<ul>
<li>参数DateField.auto_now：每次保存对象时，自动设置该字段为当前时间，用于&quot;最后一次修改&quot;的时间戳，它总是使用当前日期，默认为false</li>
<li>参数DateField.auto_now_add：当对象第一次被创建时自动设置当前时间，用于创建的时间戳，它总是使用当前日期，默认为false</li>
<li>该字段默认对应的表单控件是一个TextInput. 在管理员站点添加了一个JavaScript写的日历控件，和一个“Today&quot;的快捷按钮，包含了一个额外的invalid_date错误消息键</li>
<li>auto_now_add, auto_now, and default 这些设置是相互排斥的，他们之间的任何组合将会发生错误的结果</li>
</ul>
</li>
<li>TimeField：使用Python的datetime.time实例表示的时间，参数同DateField</li>
<li>DateTimeField：使用Python的datetime.datetime实例表示的日期和时间，参数同DateField</li>
<li>FileField：一个上传文件的字段</li>
<li>ImageField：继承了FileField的所有属性和方法，但对上传的对象进行校验，确保它是个有效的image</li>
</ul>
<p>字段选项:通过字段选项，可以实现对字段的约束,在字段对象时通过关键字参数指定</p>
<ul>
<li>null：如果为True，Django 将空值以NULL 存储到数据库中，默认值是 False</li>
<li>blank：如果为True，则该字段允许为空白，默认值是 False</li>
<li>对比：null是数据库范畴的概念，blank是表单验证证范畴的</li>
<li>db_column：字段的名称，如果未指定，则使用属性的名称</li>
<li>db_index：若值为 True, 则在表中会为此字段创建索引</li>
<li>default：默认值</li>
<li>primary_key：若为 True, 则该字段会成为模型的主键字段</li>
<li>unique：如果为 True, 这个字段在表中必须有唯一值</li>
</ul>
<p>表与表之间有三种关系：</p>
<ul>
<li>
<p>ForeignKey：一对多，将字段定义在多的端中</p>
</li>
<li>
<p>ManyToManyField：多对多，将字段定义在两端中</p>
</li>
<li>
<p>OneToOneField：一对一，将字段定义在任意一端中</p>
</li>
</ul>
<p>元选项：在模型类中定义类Meta，用于设置元信息</p>
<p>例子：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#66d9ef">def</span> <span style="color:#a6e22e">Meta</span>():
    db_table <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;user&#39;</span>  <span style="color:#75715e"># 默认为myapp_user</span>
</code></pre></div><hr>
<h3 id="创建模型代码示例">创建模型代码示例：</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#75715e"># 字段类型被定义在django.db.models.fields目录下，为了方便使用，被导入到django.db.models中</span>
<span style="color:#75715e"># 通过models.Field创建字段类型的对象，赋值给属性</span>
<span style="color:#f92672">from</span> django.db <span style="color:#f92672">import</span> models
<span style="color:#75715e"># 导入时间域</span>
<span style="color:#f92672">from</span> django.utils <span style="color:#f92672">import</span> timezone
   
<span style="color:#75715e"># 基类</span>
<span style="color:#75715e"># 这里做一个时间戳，方便用户创建信息时记录时间</span>
<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Base</span>(models<span style="color:#f92672">.</span>Model):
    <span style="color:#75715e">#时间字段</span>
    create_time <span style="color:#f92672">=</span> models<span style="color:#f92672">.</span>DateTimeField(default<span style="color:#f92672">=</span>timezone<span style="color:#f92672">.</span>now,null<span style="color:#f92672">=</span>True)
   
    <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Meta</span>:
        abstract <span style="color:#f92672">=</span> True
   
<span style="color:#75715e"># 继承基类</span>
<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">User</span>(Base):
    <span style="color:#75715e"># 用户名</span>
    username <span style="color:#f92672">=</span> models<span style="color:#f92672">.</span>CharField(max_length<span style="color:#f92672">=</span><span style="color:#ae81ff">200</span>)
    <span style="color:#75715e"># 密码</span>
    password <span style="color:#f92672">=</span> models<span style="color:#f92672">.</span>CharField(max_length<span style="color:#f92672">=</span><span style="color:#ae81ff">200</span>)
    <span style="color:#75715e"># 头像</span>
    img <span style="color:#f92672">=</span> models<span style="color:#f92672">.</span>CharField(max_length<span style="color:#f92672">=</span><span style="color:#ae81ff">200</span>)
    <span style="color:#75715e"># 用户到期(0普通用户，1超级管理员，2网站编辑)</span>
    type <span style="color:#f92672">=</span> models<span style="color:#f92672">.</span>IntegerField(default<span style="color:#f92672">=</span><span style="color:#ae81ff">0</span>,null<span style="color:#f92672">=</span>True)
    <span style="color:#75715e"># 手机号</span>
    phone <span style="color:#f92672">=</span> models<span style="color:#f92672">.</span>IntegerField()
    <span style="color:#75715e"># 个人主页</span>
    num <span style="color:#f92672">=</span> models<span style="color:#f92672">.</span>IntegerField(default<span style="color:#f92672">=</span><span style="color:#ae81ff">0</span>,null<span style="color:#f92672">=</span>True)
    <span style="color:#75715e"># 声明表名</span>
    <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Meta</span>:
        db_table <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;user&#34;</span>
</code></pre></div><p>这里一个用户表的模型就写好了，配置好数据库后，执行以下命令</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#75715e"># 首先确保在Django项目中</span>
<span style="color:#960050;background-color:#1e0010">$</span> cd mydjango
<span style="color:#75715e"># 现在在配置好数据库的前提下执行,这里的myapp是Django的应用，models所在目录</span>
<span style="color:#75715e"># 创建迁移文件</span>
<span style="color:#960050;background-color:#1e0010">$</span> python manage<span style="color:#f92672">.</span>py makemigrations myapp
<span style="color:#75715e"># 迁移文件创建完成后执行</span>
<span style="color:#75715e"># 执行迁移文件</span>
<span style="color:#960050;background-color:#1e0010">$</span> python manage<span style="color:#f92672">.</span>py migrate
</code></pre></div><p>完成以上步骤后会生成migrations文件夹，里面会有0001_initial.py文件</p>
<p>如果在执行中出现问题在删掉所有数据库后再删除migrations文件重新执行以上命令</p>
</li>
</ol>
  </div>

  <footer>
    <ul class="stats">
  <li class="categories">
    <ul>
        
            
            
                <i class="fa fa-folder"></i>
                
                
                <li><a class="article-category-link" href="/blog/blog/categories/hugo">Hugo</a></li>
                
            
        
    </ul>
  </li>
  <li class="tags">
    <ul>
        
            
            
                <i class="fa fa-tags"></i>
                
                
                <li><a class="article-category-link" href="/blog/blog/tags/tutorial">tutorial</a></li>
                
            
        
    </ul>
  </li>
</ul>

  </footer>

</article>

    <article class="post">
        <div id="disqus_thread"></div>
<script type="application/javascript">
    var disqus_config = function () {
    
    
    
    };
    (function() {
        if (["localhost", "127.0.0.1"].indexOf(window.location.hostname) != -1) {
            document.getElementById('disqus_thread').innerHTML = 'Disqus comments not available by default when the website is previewed locally.';
            return;
        }
        var d = document, s = d.createElement('script'); s.async = true;
        s.src = '//' + "shortname" + '.disqus.com/embed.js';
        s.setAttribute('data-timestamp', +new Date());
        (d.head || d.body).appendChild(s);
    })();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
    </article>


<ul class="actions pagination">
    
        <li><a href="/blog/blog/git%E5%91%BD%E4%BB%A4/"
                class="button big previous">git命令大全</a></li>
    

    
        <li><a href="/blog/blog/%E6%90%9C%E7%B4%A2%E5%8A%9F%E8%83%BD%E5%92%8C%E5%85%B3%E9%94%AE%E8%AF%8D%E4%BA%AE%E9%AB%98/"
                class="button big next">模糊搜索及关键词亮高</a></li>
    
</ul>


    </div>
    
<section id="sidebar">

  
  <section id="intro">
    
    
      
        <a href='/blog/'><img src="/blog/img/main/touxiang3.jpg" class="intro-circle" width="150px" alt="Hugo Future Imperfect" /></a>
      
    
    
      <header>
        <h2>mff</h2>
        <p>Effort is for money, and money is life!</p>
      </header>
    
    
      <ul class="icons">
        
          
    <li><a href="/blog/blog/index.xml" type="application/rss+xml" target="_blank" title="RSS" class="fa fa-rss"></a></li>


        
        












































































  <li><a href="mailto:2980738482@qq.com" title="Email" class="fa fa-envelope"></a></li>


      </ul>
    
  </section>

  
  <section class="recent-posts">
    <div class="mini-posts">
      <header>
        <h3>Recent Posts</h3>
      </header>
      <div class="posts-container">
        

        
          
        

        
          <article class="mini-post">
            <header>
              <h3>
                <a href="/blog/blog/fastdfs%E9%85%8D%E7%BD%AE%E5%8F%8A%E4%BB%8B%E7%BB%8D/">FastDFS配置及介绍</a>
              </h3>
              
              <time class="published" datetime='2020-08-27'>
                August 27, 2020
              </time>
            </header>
            

          </article>
        
          <article class="mini-post">
            <header>
              <h3>
                <a href="/blog/blog/orm%E5%B8%B8%E8%A7%84%E6%93%8D%E4%BD%9C%E9%87%8D%E7%82%B9/">orm常规操作</a>
              </h3>
              
              <time class="published" datetime='2020-07-28'>
                July 28, 2020
              </time>
            </header>
            

          </article>
        
          <article class="mini-post">
            <header>
              <h3>
                <a href="/blog/blog/%E6%94%AF%E4%BB%98%E5%AE%9D%E6%B2%99%E7%AE%B1%E6%94%AF%E4%BB%98%E6%93%8D%E4%BD%9C/">支付宝沙箱支付操作</a>
              </h3>
              
              <time class="published" datetime='2020-07-28'>
                July 28, 2020
              </time>
            </header>
            

          </article>
        
          <article class="mini-post">
            <header>
              <h3>
                <a href="/blog/blog/%E8%85%BE%E8%AE%AF%E7%9F%AD%E4%BF%A1%E5%8F%91%E9%80%81%E9%AA%8C%E8%AF%81%E7%A0%81/">腾讯短信发送验证码</a>
              </h3>
              
              <time class="published" datetime='2020-07-28'>
                July 28, 2020
              </time>
            </header>
            

          </article>
        
          <article class="mini-post">
            <header>
              <h3>
                <a href="/blog/blog/flask&#43;vue%E5%AE%9E%E7%8E%B0%E5%89%8D%E5%90%8E%E7%AB%AF%E5%88%86%E7%A6%BB/">flask&#43;vue实现前后端分离</a>
              </h3>
              
              <time class="published" datetime='2020-07-26'>
                July 26, 2020
              </time>
            </header>
            

          </article>
        
      </div>

      
        <a href=
          
            /blog/
          
        class="button">View more posts</a>
      
    </div>
  </section>

  
  
  
  
  
    <section id="categories">
      <header>
        <h3>
          <a href="/blog/categories/">Categories</a>
        </h3>
      </header>
        
          
        

        
        <p>
          <article>
            <header>
              
                <a href="/blog/categories/hugo/">hugo</a>
                <span style="float:right;">43</span>
              
            </header>
          </article>
        </p>
        
    </section>
  
  

  
  
    <section id="mini-bio">
      <h3>About</h3>
      <p>blog于2018年6月10日创建(It was created on May 10, 2020),文章主要介绍了python的一些常用框架和库及它们的一些用法</p>
      <a href="/blog/about/" class="button">Learn More</a>
    </section>
  

  
  <section id="footer">
    
      <ul class="icons">
        
          
    <li><a href="/blog/blog/index.xml" type="application/rss+xml" target="_blank" title="RSS" class="fa fa-rss"></a></li>


        
        












































































  <li><a href="mailto:2980738482@qq.com" title="Email" class="fa fa-envelope"></a></li>


      </ul>
    
    <p class="copyright">
      
        &copy; 2020
        
          mff blog
        
      .
      Powered by <a href="//gohugo.io" target="_blank">Hugo</a>
    </p>
  </section>
</section>

    </div>
    <a id="back-to-top" href="#" class="fa fa-arrow-up fa-border fa-2x"></a>
    

    
      
    

    
      
      
      
        <script src="//cdn.bootcss.com/highlight.js/9.11.0/highlight.min.js"></script>
        
        
        
        <script src="//cdn.bootcss.com/highlight.js/9.11.0/languages/r.min.js"></script>
        <script src="//cdn.bootcss.com/highlight.js/9.11.0/languages/yaml.min.js"></script>
        <script src="//cdn.bootcss.com/highlight.js/9.11.0/languages/css.min.js"></script>
        <script>hljs.configure({languages: []}); hljs.initHighlightingOnLoad();</script>
      
    
    
    
      <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/skel/3.0.1/skel.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.1.25/jquery.fancybox.min.js"></script>
      <script src="/blog/js/util.js"></script>
      <script src="/blog/js/main.js"></script>
      <script src="/blog/js/backToTop.js"></script>
    

    
      
        
      
    

    
    <script>hljs.initHighlightingOnLoad();</script>
      <script src="//yihui.name/js/math-code.js"></script>
<script async
src="//cdn.bootcss.com/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML">
</script>


  </body>
</html>

